package com.feeyo.prophet.controller;

import com.feeyo.prophet.pojo.dto.ChatRecordDTO;
import com.feeyo.prophet.pojo.enums.ResultEnum;
import com.feeyo.prophet.pojo.po.ChatRecord;
import com.feeyo.prophet.pojo.vo.ResultVO;
import com.feeyo.prophet.service.ChatRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * 聊天记录相关接口
 */
@RequestMapping("api/chat/record")
@RestController
public class ChatRecordController {

    private final ChatRecordService chatRecordService;

    @Autowired
    public ChatRecordController(ChatRecordService chatRecordService) {
        this.chatRecordService = chatRecordService;
    }

    /**
     * 获取聊天记录
     */
    @GetMapping("page")
    public ResultVO<Page<ChatRecord>> page(@RequestHeader(defaultValue = "") String appId,
                                           @RequestParam(required = false) String conversationId,
                                           @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        if (StringUtils.isBlank(appId)) {
            return ResultVO.sendError(ResultEnum.PARAM_ILLEGAL);
        }
        Page<ChatRecord> records = chatRecordService.page(appId, conversationId, pageNum, pageSize);
        return ResultVO.sendSuccess(records);
    }

    /**
     * 用户反馈
     */
    @PostMapping(value = "feedback")
    public ResultVO<Boolean> feedback(@RequestBody ChatRecordDTO chatRecord) {
        if (chatRecord == null || StringUtils.isBlank(chatRecord.getId())) {
            return ResultVO.sendError(ResultEnum.PARAM_ILLEGAL);
        }
        boolean result = chatRecordService.updateUserFeedback(chatRecord.getId(), chatRecord.getUsefulness(), chatRecord.getFeedback());
        return ResultVO.sendSuccess(result);
    }

    /**
     * 删除
     */
    @PostMapping(value = "delete")
    public ResultVO<Boolean> delete(@RequestBody ChatRecordDTO chatRecord) {
        if (chatRecord == null || StringUtils.isBlank(chatRecord.getId())) {
            return ResultVO.sendError(ResultEnum.PARAM_ILLEGAL);
        }
        boolean result = chatRecordService.delete(chatRecord.getId(), true);
        return ResultVO.sendSuccess(result);
    }

}
